<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BOperation
 *
 * @author I062261
 */
class BOperation extends BNode {

    public $name = null;
    public $id = null;
    public $className = 'BOperation';
    public $data = null;
    public $description = null;
    public $appId = null;
    public $spaceId = null;
    public $messenger = null;
    public $config = null;
    protected $currentApp = null;
    protected $currentOperationFlow = null;
    protected $isRegisteredMenu = false;

    public function __construct($item = NULL, $context = NULL) {
        parent::__construct($item, $context);
        $this->className = get_class($this);
    }

    //put your code here
    public function getUIManager($data = NULL) {
        if ($this->uiManager == NULL) {
            $this->uiManager = new BUIOperation($this);
        }
        return $this->uiManager;
    }

    /**
     * 
     * @param type $context
     * @return BMessenger
     */
    public function getMessenger($context = NULL) {
        if ($this->messenger == NULL) {
            $this->messenger = new BMessenger($this);

            $sendContext = $this->getConfigManager()->getConfig('messenger.sendContext');
            $receiveContext = $this->getConfigManager()->getConfig('messenger.receiveContext');

            if ($sendContext != NULL)
                $this->messenger->addSendContext($sendContext);
            if ($receiveContext != NULL)
                $this->messenger->addReceiveContext($receiveContext);
        }

        if ($context != NULL && is_array($context)) {
            if (isset($context['sendContext']))
                $this->messenger->addSendContext($context['sendContext']);
            if (isset($context['receiveContext']))
                $this->messenger->addReceiveContext($context['receiveContext']);
        }

        return $this->messenger;
    }

    public function run($data = NULL) {
        if (Blyn::app()->getActiveApp() instanceof BApp) {
            $app = Blyn::app()->getActiveApp();
            if (method_exists($app, 'registerOperationMenu')) {
                $app->registerOperationMenu($this);
            }
            $method = 'run' . ucfirst($this->getName());
            if (method_exists($app, $method)) {
                $app->$method($this, $data);
            }

            //first set active link
            if (yii::app()->request->isAjaxRequest) {
                if (isset($_REQUEST['b_ajaxLinkId']) && ($_REQUEST['b_ajaxLinkId'] != NULL)) {
                    $ajaxLinkId = $_REQUEST['b_ajaxLinkId'];
                    Blyn::app()->getUIManager()->setActiveLink($ajaxLinkId);
                }
            } else {
                if (isset($_REQUEST['b_linkId']) && ($_REQUEST['b_linkId'] != NULL)) {
                    $linkId = $_REQUEST['b_linkId'];
                    Blyn::app()->getUIManager()->setActiveLink($linkId);
                }
            }

            $activeLink = Blyn::app()->getUIManager()->getActiveLink();

            if ($activeLink instanceof BUILink) {
                //adjust theme if specify
                $theme = $activeLink->getLinkVariable('theme');
                if ($theme != NULL) {
                    Blyn::app()->getUIManager()->setTheme($theme);
                }
                //add cooker like controller/action
                $cooker = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('cooker');
                if (is_string($cooker) && $cooker != NULL) {
                    //$pattern = "/[\\/|\\]/";
                    $cookerList = preg_split('/[\\\|@#\/]/', $cooker);
                    /*
                      $cookerList = explode('/', $cooker);
                      if($count($cookerList)<2){
                      $cookerList = explode("\\",$cooker);
                      }
                     * 
                     */
                    $count = count($cookerList);
                    if ($count > 1) {
                        $cookerClass = $cookerList[0];
                        $cookerMethod = $cookerList[1];
                    } else {
                        $cookerClass = $cooker;
                        $cookerMethod = 'index';
                    }
                    if (strpos($cookerClass, 'Cooker') === FALSE) {
                        $cookerClass.='Cooker';
                    }
                    if (class_exists($cookerClass)) {
                        $cooker = new $cookerClass();
                        if (method_exists($cooker, 'on' . ucfirst($cookerMethod))) {
                            $cookerMethod = 'on' . ucfirst($cookerMethod);
                            $cooker->$cookerMethod();
                        } else if (method_exists($cooker, $cookerMethod)) {
                            $cooker->$cookerMethod();
                        }
                    }
                }
            }

            //call run for register controls
            $screen = Blyn::app()->getUIManager()->getCurrentScreen();
            $registerControls = $screen->getChildrenOfControl();
            foreach ($registerControls as $control) {
                if (method_exists($control, 'run')) {
                    $control->run($this);
                }
            }
        }
    }

    /**
     * app always belong to some app
     * @param type $data
     */
    public function getApp($data = NULL) {
        if ($this->appId != NULL)
            return BApp::getAppById($this->appId);
    }

    /**
     * 
     * @param type $data
     * @return BApp
     */
    public function getCurrentApp($data = NULL) {
        if (!$this->currentApp instanceof BApp)
            $this->currentApp = $this->getApp();
        return $this->currentApp;
    }

    public function setCurrentApp($app) {
        $this->currentApp = $app;
    }

    public function getCurrentOperationFlow($data = NULL) {
        return $this->currentOperationFlow;
    }

    public function setCurrentOperationFlow($opFlow) {
        $this->currentOperationFlow = $opFlow;
    }

    public function getMenu($name = 'root') {

        $root = $this->getAttribute('register_menu_root', array('spaceId' => Blyn::app()->getActiveSpace()->getId()));
        if (!$root instanceof BAttribute) {
            $root = $this->addAttribute('register_menu_root', 'register menu root for operation');
            $root->spaceId = Blyn::app()->getActiveSpace()->getId();
            $root->save();
        }

        if ($name == NULL || $name == 'root') {
            return $root;
        } else {
            return $root->getAttribute($name);
        }
    }

}

?>
